package removeDuplicateNodes;

import java.util.HashSet;
import java.util.Set;

public class Solution {
      public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }
    public ListNode removeDuplicateNodes(ListNode head) {
          // 移除重复元素
        if(head==null)return head;
        Set<Integer> set = new HashSet<>();
        ListNode cur = head;
        set.add(cur.val);
        while (cur.next!=null) {
            if(set.contains(cur.next.val)) {
                cur.next = cur.next.next;
            } else {
                set.add(cur.next.val);
                cur = cur.next;
            }
        }
        return head;
    }
}
